var flashphoner;

var callee = '';
var callerLogin = '';
var registerRequired;
var isLogged = false;

var micVolume = 100;
var speakerVolume = 100;
var connectingViewBeClosed = false;
var traceEnabled = true;

function trace(str){
	if (traceEnabled){
		getElement("console").innerHTML=getElement("console").innerHTML+"\n"+str;
	}
}

function login(){
	trace("login");
	connectingViewBeClosed = false;
	callerLogin = document.getElementById('username').value;
	var result = flashphoner.login("sip:"+callerLogin+"@"+document.getElementById('server').value+":"+document.getElementById('port').value,document.getElementById('password').value);
	closeLoginView();
	openConnectingView("Connecting...",0);
}
function logoff(){
	trace("logoff");
	flashphoner.logoff();
}

function call(){
	trace("call");
	if (isLogged){
		var result = flashphoner.call(getElement('calleeText').value,'Caller');
		if (result == 0){
			toHangupState();
		}else{
			openConnectingView("Callee number is wrong", 3000);
		}
	}else{
		openLoginView();
	}
}

function answer(){
	trace("answer");
	flashphoner.answer();
}

function hangup(){
	trace("hangup");
	flashphoner.hangup();
	getElement('callButton').style.backgroundColor = "gray";
	getElement('callButton').disabled="disabled";
}

//notified function
function notifyRegisterRequired(registerR){
	registerRequired = registerR;
}

function notifyCloseConnection(){
	trace("notifyCloseConnection");
	toLogOffState();
	isLogged = false;
	closeIncomingView();
	toCallState();
}

function notifyConnected(){
	trace("notifyConnected");
	if (registerRequired){
		if (!connectingViewBeClosed){
			openConnectingView("Waiting register event", 0);
		}
	}else{
		toLogState();
		getElement("loggedUserDiv").innerHTML=callerLogin;
		isLogged = true;
		closeConnectingView();
	}
}

function notifyRegistered(){
	trace("notifyRegistered");
	if (registerRequired){
		toLogState();
		getElement("loggedUserDiv").innerHTML=callerLogin;
		isLogged = true;
		closeConnectingView();
	}
}

function notifyBalance(balance){
	
}

function notify(state){
	
	if (state == STATE_BUSY){
		toCallState();
	}else if (state == STATE_FINISH){
		toCallState();
		closeIncomingView();
	}	
}

function notifyCost(cost){
	
}

function notifyCall(caller,visibleNameCaller){
	trace("notifyCall: caller "+caller+" visibleNameCaller "+visibleNameCaller);
	openIncomingView(caller,visibleNameCaller);
	toHangupState();
}

function notifyError(error){

	trace("notifyError: error "+error);

	if (error == CONNECTION_ERROR){	
		openConnectingView("Connection fail",3000);
	} else if (error == AUTHENTICATION_FAIL){				
		openConnectingView("Register fail",3000);
		window.setTimeout("logoff();", 3000);
	} else if (error == USER_NOT_AVAILABLE){
		openConnectingView("Callee not found!",3000);
	} else if (error == TOO_MANY_REGISTER_ATTEMPTS){
		openConnectingView("Connection error",3000);
		toLoggedOffState();
	} else if (error == LICENSE_RESTRICTION){
		openConnectingView("License restriction",3000);
	} else if (error == INTERNAL_SIP_ERROR){
		openConnectingView("Unknown error",3000);
	} else if (error == REGISTER_EXPIRE){
		openConnectingView("Check SIP account settings",3000);
	} else if (error == SIP_PORTS_BUSY){
		openConnectingView("All sip ports is busy",3000);
		connectingViewBeClosed = true;
		window.setTimeout("logoff();", 3000);
	} else if (error == MEDIA_PORTS_BUSY){
		openConnectingView("All media ports is busy",3000);
	} else if (error == WRONG_SIPPROVIDER_ADDRESS){
		openConnectingView("Wrong sip provider address",3000);
		connectingViewBeClosed = true;
		window.setTimeout("logoff();", 3000);
	}			
	toCallState();
} 

//additional functions
function toLogState(){
	trace("toLogState");
	getElement('loginMainButton').value = "Log out";
}

function toLogOffState(){
	trace("toLogOffState");
	getElement('loginMainButton').value = "Log in";
	getElement("loggedUserDiv").innerHTML = "";
}

function toHangupState(){
	trace("toHangupState");
	getElement('callButton').value = "Hangup";
	disableCallButton();	
}

function toCallState(){	
	trace("toCallState");
	getElement('callButton').value = "Call";
	disableCallButton();	
}
function disableCallButton(){
	trace("disableCallButton");
	window.setTimeout("getElement('callButton').disabled='';	getElement('callButton').style.backgroundColor = '#009800';", 3000);
	getElement('callButton').style.backgroundColor = "gray";
	getElement('callButton').disabled="disabled";
}
function openLoginView(){
	trace("openLoginView");
	getElement('loginDiv').style.visibility = "visible";
}
function closeLoginView(){
	trace("closeLoginView");
	getElement('loginDiv').style.visibility = "hidden";
}

function openConnectingView(str,timeout){
	trace("openConnectingView: str "+str+" timeout "+timeout);
	if (timeout != 0){
		window.setTimeout("closeConnectingView();", timeout);
	}
	getElement('connectingDiv').style.visibility = "visible";
	getElement('connectingText').innerHTML = str;
}
function closeConnectingView(){
	trace("closeConnectingView");
	getElement('connectingDiv').style.visibility = "hidden";
}

function openIncomingView(caller,visibleNameCaller){
	trace("openIncomingView: caller "+caller+" visibleNameCaller "+visibleNameCaller);
	getElement('incomingDiv').style.visibility = "visible";
	getElement('callerField').innerHTML = caller+" '"+visibleNameCaller+"'";

	
}
function closeIncomingView(){
	trace("closeIncomingView");
	getElement('incomingDiv').style.visibility = "hidden";
}

function openSettingsView(){
	trace("openSettingsView");
	getElement('settingsDiv').style.visibility = "visible";
}
function closeSettingsView(){
	trace("closeSettingsView");
	getElement('settingsDiv').style.visibility = "hidden";
}
function saveMicSettings(){
	trace("saveMIcSettings");
	flashphoner.setVolume(speakerVolume);
	flashphoner.setMicVolume(micVolume);
	closeSettingsView();
}
function getElement(str){
	return document.getElementById(str);	
}

